package com.design.springboot.booksharing.controller;

import com.design.springboot.booksharing.bean.*;
import com.design.springboot.booksharing.component.ApiResultComponent;
import com.design.springboot.booksharing.enums.ApiResultCodeEnum;
import com.design.springboot.booksharing.service.UserService;
import com.design.springboot.booksharing.utils.endecrypt.Md5Utils;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户信息表(User)控制器
 *
 * @author admin
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    ApiResultComponent apiResultComponent;

    @Resource
    private UserService userService;

    @PostMapping("/saveUser")
    public ApiResult<User> saveUser(User user) {
        String password = Md5Utils.hash(user.getPassword());
        user.setPassword(password);

        Boolean saveUser = userService.saveUser(user);
        if (saveUser) {
            User userById = userService.getUserById(user.getId());
            return apiResultComponent.success(userById);
        }
        return apiResultComponent.error(ApiResultCodeEnum.ADD_ERROR);
    }

    @PostMapping("/updateUser")
    public ApiResult<Integer> updateUser(User user) {
        String userPassword = user.getPassword();
        if (StringUtils.isNotEmpty(userPassword)) {
            String password = Md5Utils.hash(userPassword);
            user.setPassword(password);
        }

        Boolean updateUser = userService.updateUser(user);
        if (updateUser) {
            User userById = userService.getUserById(user.getId());
            return apiResultComponent.success(userById);
        }
        return apiResultComponent.error(ApiResultCodeEnum.UPDATE_ERROR);
    }

    @PostMapping("/updateUserPassword")
    public ApiResult<Void> updateUserPassword(Integer userId, String oldPassword, String newPassword) {
        User user = userService.getUserById(userId);
        if (user != null) {
            String password = Md5Utils.hash(oldPassword);

            if (user.getPassword().equals(password)) {
                user.setPassword(Md5Utils.hash(newPassword));
                Boolean updateUser = userService.updateUser(user);
                if (updateUser) {
                    return apiResultComponent.success();
                }
            }
        }
        return apiResultComponent.success(ApiResultCodeEnum.USER_NULL);
    }

    @PostMapping("/deleteUser")
    public ApiResult<User> deleteUser(Integer userId) {
        User userById = userService.getUserById(userId);
        if (userById == null) {
            return apiResultComponent.error(ApiResultCodeEnum.USER_NULL);
        }
        userService.deleteUser(userId);
        return apiResultComponent.success(userById);
    }

    @GetMapping("/getUserInfo")
    public ApiResult<User> getUserInfo(@RequestAttribute("activeUser") ActiveUser activeUser) {
        try {
            Integer id = activeUser.getUserId();
            User userById = userService.getUserById(id);

            User user = new User();
            user.setUsername(userById.getUsername());
            user.setSex(userById.getSex());
            user.setUpdateTime(userById.getUpdateTime());
            return apiResultComponent.success(user);
        } catch (Exception ex) {
            ex.printStackTrace();
            log.error(ex.getMessage(), ex);
            return apiResultComponent.error(ApiResultCodeEnum.ERROR);
        }
    }

    @GetMapping("/getUserById")
    public ApiResult<User> getUserById(Integer id) {
        User userById = userService.getUserById(id);
        if (userById == null) {
            return apiResultComponent.error(ApiResultCodeEnum.USER_NULL);
        }
        return apiResultComponent.success(userById);
    }

    @GetMapping("/listUser")
    public ApiResult<Pagination<User>> listUser(User user,
        @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
        @RequestParam(value = "pageSize", defaultValue = "0") Integer pageSize,
        @RequestParam(value = "sortField", required = false) String sortField,
        @RequestParam(value = "order", required = false) String order) {

        QueryWarp<User> userQueryWarp = new QueryWarp<>();
        userQueryWarp.put(user);

        if (StringUtils.isNotEmpty(sortField) && StringUtils.isNotEmpty(order)) {
            userQueryWarp.put("sortField", sortField);
            userQueryWarp.put("order", order);
        }

        PageHelper.startPage(pageNum, pageSize);
        List<User> userList = userService.listUser(userQueryWarp);
        return apiResultComponent.success(userList);
    }
}
